package com.qf.service;

import com.qf.dao.StudentDao;
import com.qf.dao.StudentDaoImpl;
import com.qf.pojo.Student;
import com.qf.util.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * 业务层实现类
 * @author 千锋健哥
 */
public class StudentServiceImpl implements StudentService {

    /**
     * 全局dao变量, 可以这个类中的所有方法公用
     */
    private StudentDao studentDao = new StudentDaoImpl();

    @Override
    public List<Student> findStudentAll() {
        List<Student> list = studentDao.findAll();
        return list;
    }

    /**
     * 批量添加学员数据
     * @param studentList
     */
    @Override
    public void addStudentList(List<Student> studentList) {
        try {
            //2. 开启事务, 关闭事务自动提交
            DBUtils.beginTransaction();
            //3. 处理业务, 批量添加
            if (studentList != null) {
                for (Student student : studentList) {
                    studentDao.insertStudent(student);
                }
            }
            //4. 提交事务
            DBUtils.commitTransaction();
        } catch (Exception e) {
            //打印异常堆栈信息
            e.printStackTrace();
            //5. 如果代码有异常则回滚数据
            DBUtils.rollbackTransaction();
        }

    }

}
